package com.itheima.socket.tcp.demo11;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * TCP客户端代码：客户端读取视频文件数据发送到服务端，服务器端读取到数据以后，将数据存储到文件中
 */
public class ClientDemo01 {

    public static void main(String[] args) throws Exception {

        // 1. 创建Socket对象
        Socket socket = new Socket(InetAddress.getByName("169.254.106.206") , 8080) ;

        // 2. 获取输出流对象
        OutputStream outputStream = socket.getOutputStream();

        // 3. 创建输入流对象，读取数据(数据来自于文件) , 选择字节流，而一般我们使用的都是高效的字节输入流对象
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\a.avi")) ;
        byte[] bytes = new byte[1024] ;
        int len = -1 ;
        while((len = bufferedInputStream.read(bytes)) != -1) {

            // 4. 把数据写出去
            outputStream.write(bytes , 0 , len);

        }

        //通知服务端输出流已经关闭了
        socket.shutdownOutput();

        // 获取输入流对象
        InputStream inputStream = socket.getInputStream();
        byte[] result = new byte[1024] ;
        int length = inputStream.read(result);
        System.out.println(new String(result , 0 , length , "UTF-8"));

        // 5. 释放资源
        bufferedInputStream.close();
        socket.close();

    }

}
